package com.young.springboot.httpclient;

import com.young.commom.httpclient.HttpClientConfiguration;
import com.young.common.context.ContextConstants;
import com.young.common.context.HttpConstants;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 2:38 PM
 */
@ConfigurationProperties("spring.http-client")
public class HttpClientProperties {

    private String charset = ContextConstants.DEFAULT_CHARSET;

    //连接创建，HTTP/TCP等RPC
    private Duration connectionTimeout = Duration.ofMillis(HttpClientConfiguration.DEFAULT_CONNECTION_TIMEOUT);

    //SO_TIMEOUT
    private Duration socketTimeout = Duration.ofMillis(HttpClientConfiguration.DEFAULT_SOCKET_TIMEOUT);

    //连接池中的连接被保活的时长
    private Duration keepAliveTime = Duration.ofMillis(HttpClientConfiguration.DEFAULT_KEEP_ALIVE_TIME);

    private Duration requestTimeout = Duration.ofMillis(HttpClientConfiguration.DEFAULT_REQUEST_TIMEOUT);

    //请求异常（exception），重试的次数，默认为0，不重试
    private Integer retryTimes = HttpClientConfiguration.DEFAULT_HTTP_RETRY_TIMES;
    //是否重试
    private Boolean retryOnFailure = HttpClientConfiguration.DEFAULT_HTTP_RETRY_ON_FAILURE;

    //业务全局header，默认会添加到http下游调用的请求中,每项为K-V
    private Map<String, String> globalHeaders;

    private String type = HttpConstants.HTTP_CLIENT_TYPE_HTTP_COMPONENTS;

    private DnsResolver dnsResolver = new DnsResolver();

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Duration getConnectionTimeout() {
        return connectionTimeout;
    }

    public void setConnectionTimeout(Duration connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public Duration getSocketTimeout() {
        return socketTimeout;
    }

    public void setSocketTimeout(Duration socketTimeout) {
        this.socketTimeout = socketTimeout;
    }

    public Duration getKeepAliveTime() {
        return keepAliveTime;
    }

    public void setKeepAliveTime(Duration keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public Integer getRetryTimes() {
        return retryTimes;
    }

    public void setRetryTimes(Integer retryTimes) {
        this.retryTimes = retryTimes;
    }

    public Boolean getRetryOnFailure() {
        return retryOnFailure;
    }

    public void setRetryOnFailure(Boolean retryOnFailure) {
        this.retryOnFailure = retryOnFailure;
    }

    public Map<String, String> getGlobalHeaders() {
        return globalHeaders;
    }

    public void setGlobalHeaders(Map<String, String> globalHeaders) {
        this.globalHeaders = globalHeaders;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Duration getRequestTimeout() {
        return requestTimeout;
    }

    public void setRequestTimeout(Duration requestTimeout) {
        this.requestTimeout = requestTimeout;
    }

    public DnsResolver getDnsResolver() {
        return dnsResolver;
    }

    public static class DnsResolver {
        private boolean enabled;

        private Map<String,String> domain;

        private Map<String,String> service;

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }

        public Map<String, String> getDomain() {
            return domain;
        }

        public void setDomain(Map<String, String> domain) {
            this.domain = domain;
        }

        public Map<String, String> getService() {
            return service;
        }

        public void setService(Map<String, String> service) {
            this.service = service;
        }
    }

}
